package cn.xopencode.appletweb.controller.record.maintransformer;

import cn.xopencode.common.framework.util.StringUtils;
import cn.xopencode.common.framework.vo.CommonResult;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.recordservice.rpc.maintransformer.MainTransformerRecordRpc;
import cn.xopencode.recordservice.rpc.maintransformer.dto.MainTransformerRecordCreateDTO;
import cn.xopencode.recordservice.rpc.maintransformer.dto.MainTransformerRecordPageDTO;
import cn.xopencode.recordservice.rpc.maintransformer.dto.MainTransformerRecordUpdateDTO;
import cn.xopencode.recordservice.rpc.maintransformer.vo.MainTransformerRecordAdminPageVO;
import cn.xopencode.recordservice.rpc.maintransformer.vo.MainTransformerRecordEmpRelVO;
import cn.xopencode.recordservice.rpc.maintransformer.vo.MainTransformerRecordPageVO;
import cn.xopencode.recordservice.rpc.maintransformer.vo.MainTransformerRecordUpdateVO;
import cn.xopencode.recordservice.rpc.robotroom.dto.AdminRecordPageDTO;
import cn.xopencode.userservice.rpc.dept.DeptRpc;
import cn.xopencode.userservice.rpc.user.UserRpc;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "主变巡检记录 API")
@RestController
@RequestMapping("/maintransformer")
@Validated
public class MainTransformerRecordController {

    @DubboReference(version = "${dubbo.consumer.MainTransformerRecordRpc.version}")
    private MainTransformerRecordRpc mainTransformerRecordRpc;

    @DubboReference(version = "${dubbo.consumer.UserRpc.version}")
    private UserRpc userRpc;

    @DubboReference(version = "${dubbo.consumer.DeptRpc.version}")
    private DeptRpc deptRpc;
    /**
     * 根据查询条件 分页获取所有的主变巡检记录
     * @param pageDTO
     * @return
     */
    @PostMapping("/getAllByQuery")
    @ApiOperation("根据查询条件分页获取所有的主变巡检记录")
    public CommonResult<PageResult<MainTransformerRecordPageVO>> getPageMainTransformerRecord(@Valid MainTransformerRecordPageDTO pageDTO){
        CommonResult<PageResult<MainTransformerRecordPageVO>> result = mainTransformerRecordRpc.pageMainTransformerRecord(pageDTO);
        List<MainTransformerRecordPageVO> list = result.getData().getList();
        list.forEach(pageVO -> {
            if(!StringUtils.isEmpty(pageVO.getEmpIds())){
                pageVO.setEmpRels(userRpc.getEmployeeName(pageVO.getEmpIds()));
            }
        });
        return result;
    }

    /**
     *  新增 主变巡检记录
     * @param roomDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增 主变巡检记录")
    public CommonResult<Integer> createRobotRoomRecord(@Valid MainTransformerRecordCreateDTO roomDTO){
        return mainTransformerRecordRpc.createMainTransformerRecord(roomDTO);
    }

    /**
     *  更新 主变巡检记录
     * @param roomDTO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("更新 主变巡检记录")
    public CommonResult<Integer> updateMainTransformerRecord(@Valid MainTransformerRecordUpdateDTO roomDTO){
        return mainTransformerRecordRpc.updateMainTransformerRecord(roomDTO);
    }

    /**
     * 根据id获取主变巡检记录详细
     * @param mainTransformerRecordId
     * @return
     */
    @GetMapping("/getById")
    @ApiOperation("根据id获取主变巡检记录详细")
    public CommonResult<MainTransformerRecordUpdateVO> getMainTransformerRecordById(String mainTransformerRecordId){
        CommonResult<MainTransformerRecordUpdateVO> result = mainTransformerRecordRpc.getMainTransformerRecordById(mainTransformerRecordId);
        MainTransformerRecordUpdateVO updateVO = result.getData();
        if(updateVO != null){
            List<String> list = updateVO.getEmpList().stream().map(MainTransformerRecordEmpRelVO::getEmpId).collect(Collectors.toList());
            String empNames = userRpc.getEmployeeName(StringUtils.join(list, ","));
            updateVO.setEmpNames(empNames);
        }
        return result;
    }

    /**
     * 处理 主变巡检记录 中的异常，在巡检记录中只是改变状态
     * @param recordId
     * @return
     */
    @PostMapping("/deal")
    @ApiOperation("处理 主变巡检记录 中的异常")
    public CommonResult<Integer> dealMainTransformerRecordException(@RequestParam(required = true) String recordId){
        return mainTransformerRecordRpc.dealMainTransformerRecordException(recordId);
    }


    /**
     * 后台--根据查询条件 分页获取所有的主变巡检记录
     * @param pageDTO
     * @return
     */
    @PostMapping("/getAllAdminByQuery")
    @ApiOperation("后台--根据查询条件分页获取所有的主变巡检记录")
    public CommonResult<PageResult<MainTransformerRecordAdminPageVO>> getAdminPageMainTransformerRecord(AdminRecordPageDTO pageDTO){
        String level2DeptId = pageDTO.getLevel2DeptId();
        String deptId = pageDTO.getDeptId();
        if(!StringUtils.isEmpty(level2DeptId)){
            pageDTO.setQueryDeptId(deptRpc.getDeptIdList(level2DeptId).getData());
        }else if(!StringUtils.isEmpty(deptId)){
            pageDTO.setQueryDeptId(deptRpc.getDeptIdList(deptId).getData());
        }
        CommonResult<PageResult<MainTransformerRecordAdminPageVO>> result = mainTransformerRecordRpc.getAdminPageMainTransformerRecord(pageDTO);
        List<MainTransformerRecordAdminPageVO> list = result.getData().getList();
        list.forEach(pageVO -> {
            if(!StringUtils.isEmpty(pageVO.getEmpIdStr()))
                pageVO.setEmpNames(userRpc.getEmployeeName(pageVO.getEmpIdStr()));
        });
        return result;
    }

}
